<?php
namespace BoxEdu\Controller\Admin;
//use Think\Controller;

class QuestionsController extends CommonController
{

    public function _initialize()
    {
        parent::_initialize();
        $data = array(
            'questsort' => array('1' => '主观题', 2 => '客观题', 3 => '冒题'),
            'questchoice' => array('1' => '单选', '2' => '多选', '3' => '不定项选', '4' => '判断', '5' => '分录题'),
        );
        $data2 = array(
            '1' => '易',
            '2' => '中',
            '3' => '难',
        );
        $data3 = array(
            'autopage' => '随机组卷',
            'selfpage' => '手工组卷',
            'module_exam' => '章节练习',
            'knows_exam' => '小节练习',
        );
        dict("questype_class", $data);
        dict("questionlevel", $data2);
        dict("exams_type", $data3);
        $this->questype_class = dict("questype_class");
        $this->questionlevel = dict("questionlevel");
        $this->exam_type = dict("exams_type");
        $type = I('type');
        if ($type == 2) {
            if ($questionparent = I('questionparent')) {
                $this->questiontype_list = BoxModel('edu_questype')->where("questsort<>3 and is_show=1")->select();
            } else {
                $this->questiontype_list = BoxModel('edu_questype')->where("questsort=3 and is_show=1")->select();
            }
            $_GET['memuNav'] = 'rowsquestions';
        } else {
            $this->questiontype_list = BoxModel('EduQuestions')->questype();
        }
								 $this->subject_list = BoxModel("EduSubject")->tree();
    }

    /****题型管理**/
    public function questype()
    {
        $this->data = BoxModel('EduQuestions')->questype();
        $this->display();
    }

    /****题型管理**/
    public function add_questype()
    {
        $this->memuNav = "questype";
        $model = BoxModel("edu_questype");
        $id = I('id');
        if (IS_POST) {
            $row = $model->create();
            if ($id) {
                $row['questid'] = $id;
                $model->save($row);
                $this->jump("保存成功");
            } else {
                $model->add($row);
                $this->jump("保存成功", U('questype'));
            }
        }
        if ($id) {
            $this->data = $model->find($id);
        }
        $this->display();
    }

    public function del_questype()
    {
        $id = I('id');
        $r = BoxModel("edu_questype")->where(array('questid' => $id))->save(array('is_show' => 0));
        $this->jump("删除成功");
    }


    /******普通问题****/
    public function index()
    {
       // $this->subject_list = BoxModel("EduSubject")->get();
        $this->type = I('request.type', "1", "intval");
        $this->title = "普通问题";
        $this->pid = I('request.pid');
        $this->piddata = BoxModel('EduQuestions')->load($this->pid);
        if ($this->piddata) {
            $this->type = $this->piddata['type'];
        }
        /*******冒题pid***/
        if ($this->type == 2) {
            $this->memuNav = "rowsquestions";
            $this->title = "冒题管理";
        }
        $where = "Q.questionstatus=1";
        if ($this->pid) {
            $where .= " and Q.questionparent='$this->pid'";
        } else if ($this->type == 2) {
            $where .= " and Q.type='$this->type' and Q.questionparent=0";
        } else {
            $where .= " and  Q.questionparent=0";
        }
        if ($this->kw = I('request.kw')) {
            $where .= " and (Q.question like '%$this->kw%' or questionlastmodifyuser  like '%$this->kw%')";
        }
        if ($this->questiontype = I('request.questiontype')) {
            $where .= " and Q.questiontype='$this->questiontype'";
        }
        if ($this->questionid = I('request.questionid')) {
            $where .= " and Q.questionid='$this->questionid'";
        }

        if ($this->squestionlevel = I('request.questionlevel')) {
            $where .= " and Q.questionlevel='$this->squestionlevel'";
        }

        if ($this->questionsubjectid = I('request.questionsubjectid')) {
            $where .= " and K.qkquestionsubjectid='$this->questionsubjectid'";
            $this->sections_list = BoxModel("EduSubject")->get_section_by_subjectid($this->questionsubjectid);
        }
        // dump($this->questionsubjectid);
        if ($this->questionsectionid = I('request.questionsectionid')) {
            $where .= " and K.questionsectionid='$this->questionsectionid'";
            $this->knows_list = BoxModel("edu_knows")->get("knowssectionid='$this->questionsectionid'");
        }
        if ($this->questionknowsid = I('request.questionknowsid')) {
            $where .= " and K.qkknowsid='$this->questionknowsid'";
        }


        if ($this->stime = I('request.stime')) {
            $where .= " and Q.add_time>'$this->stime'";
        }
        if ($this->etime = I('request.etime')) {
            $where .= " and Q.add_time<'$this->etime'";
        }
        // dump($where);
        $data = BoxModel('EduQuestions')->get($where, 10);
        // dump(BoxModel('EduQuestions'));
        //dump(BoxModel('EduQuestions'));
        $this->data = $data['list'];
        $this->show_page($data['pager']);
        // dump($data['pager']);
        // $this->page=$data['pager']->show;
        $this->select_tpl = $tpl = I('request.select_tpl');        //选题的
        $this->display($tpl . "index");
    }

    /******添加普通试题****/
    public function addquestion()
    {
        $this->memuNav = "index";
         
        $questionid = I('questionid');
        $this->questionparent = I('questionparent');
        $this->data = BoxModel('EduQuestions')->load($questionid);
//	dump($this->data);
        $this->type = I('type');
        /****冒题信息*****/
        $this->pid = I('questionparent');
        $this->piddata = BoxModel('EduQuestions')->load($this->pid);
        /*******冒题pid***/
        if ($this->data) {
            $this->pid = $this->questionparent = $this->data['questionparent'];
            $this->type = $this->data['type'];
        }
        /*******冒题pid***/
        if ($this->type == 2) {
            $this->memuNav = "rowsquestions";
            $this->title = "冒题管理";
        }
        if (IS_POST) {
            $row = BoxModel('EduQuestions')->create();
            $row['questionid'] = $questionid;
            $questiontype = I('questiontype');
            if ($questiontype) {
                $questiontypedata = BoxModel("edu_questype")->where("questid='$questiontype'")->find();
            }
            /**分录题**/
            if ($questiontypedata['questchoice'] == 5) {
                $row['questionanswer'] = I('questionanswer_5');
            }
            //dump($row);exit();
            $questionid = $this->act_add_question($row);
            /***********题库对应的知识点结束了*******************/
            $url = U('Questions/addquestion', array('type' => $this->type, 'questionid' => $questionid));
            $this->jump("保存成功", $url);
        }
        $this->display();
    }

    function act_add_question($row)
    {
        $row['questionanswer'] = json_encode($row['questionanswer']);
        $row['questionusername'] = session('admin_user_name');
        $questionid = $row['questionid'];
        if ($questionid) {
            BoxModel('EduQuestions')->where(array('questionid' => $questionid))->save($row);
        } else {
            unset($row['questionid']);
            $row['add_time'] = date("Y-m-d H:i:s");
            $questionid = BoxModel('EduQuestions')->add($row);
        }
        /*********************题库对应的知识点************/
        BoxModel("edu_quest2knows")->where("qkquestionid='$questionid'")->delete();
        $questionsubjectids = I('questionsubjectid');
        $qkknowsids = I('qkknowsid');
        $questionsectionids = I('questionsectionid');
        shuffle($questionsubjectids);
        shuffle($qkknowsids);
        shuffle($questionsectionids);
        if (is_array($questionsubjectids)) {
            foreach ($questionsubjectids as $k => $one) {
                $row = array(
                    'questionsectionid' => intval($questionsectionids[$k]),
                    'qkquestionid' => $questionid,
                    'qkknowsid' => intval($qkknowsids[$k]),
                    'qkquestionsubjectid' => intval($questionsubjectids[$k]),
                );
                BoxModel("edu_quest2knows")->add($row);
            }
        }
        return $questionid;
    }

    /******添加普通试题****/
    public function import_question()
    {
        $this->subject_list = BoxModel("EduSubject")->tree();;
        $this->display();
    }


    public function importdo()
    {
        header("Content-type: text/html; charset=utf-8");
        if (IS_POST) {
            $file = $_FILES['excel']['tmp_name'];
            $model = Logic("BoxEdu", "User");
            if ($file) {
                ini_set('memory_limit', '1280M');
                set_time_limit(0);
                $obj = new \Common\Org\icPHPExcel();
                // $obj->Load_Excel($file);
                //$data = $obj->findAll();
                $data = $obj->getDataFromExcel($file);
                $fields = array();
                $sucees = 0;
                $fail = 0;
                $i = 1;
                foreach ($data[0] as $k => $one) {
                    if (preg_match("/^[a-z]/i", $one)) {
                        $fields[$k] = array(
                            'fields' => 'questionanswer',
                            'value' => $one,
                        );
                    }
                    if ($one == "题目") {
                        $fields[$k] = "question";
                    }
                    if ($one == "题型") {
                        $fields[$k] = "questiontype";
                    }
                    if ($one == "答案") {
                        $fields[$k] = "answer";
                    }
                    if ($one == "解析") {
                        $fields[$k] = "questiondescribe";
                    }
                }
                unset($data[0]);
                if ($data) {
                    foreach ($data as $key => $value) {
                        $row = $this->get_row($fields, $value);
                        if ($value['0']) {
                            $questionid = $this->act_add_question($row);
                            if ($questionid) {
                                $sucees++;
                                $r = "题号：" . $value['0'] . "导入成功";
                                $class = "suc";
                            } else {
                                $fail++;
                                $r = "题号：" . $value['0'] . "导入失败";
                                $class = "fail";
                            }
                            $msg .= "<p class='" . $class . "'>" . $r . "</p>";
                        }
                    }

                    $this->msg = $msg;
                    $this->sucees = $sucees;
                    $this->fail = $fail;
                    $this->display("import_msg");
                }
            }
        }
    }

    /***********题库导入，变成数据库中的字段***/
    function get_row($fields, $data)
    {
        foreach ($data as $k => $one) {
            if (is_array($fields[$k])) {
                $row[$fields[$k]['fields']][$fields[$k]['value']] = array('text' => $one);
            } else if ($fields[$k]) {
                $row[$fields[$k]] = $one;
            }
        }
        $qtype = array(
            '判断' => 3,
            '单选' => 1,
            '多选' => 2,
        );
        $ex_answer = explode(" ", trim($row['answer']));

        foreach ($ex_answer as $one) {
            if ($one) $answer[] = $one;
        }
        $i = 1;
        foreach ($row['questionanswer'] as $k => $one) {
            if (in_array($k, $answer)) {
                $one['choose'] = 1;
            } else {
                $one['choose'] = 0;
            }

            $questionanswer[$i] = $one;
            $i++;
        }
        $row['questionanswer'] = $questionanswer;
        $row['questiontype'] = $qtype[$row['questiontype']];
        unset($row['answer']);
        //dump($row);exit();
        return $row;
    }

    public function del_question()
    {
        $questionid = I('questionid');
        $this->data = BoxModel('EduQuestions')->load($questionid);
        BoxModel('EduQuestions')->where("questionid='$questionid'")->save(array('questionstatus' => 2));
        $this->jump("删除成功");

    }

    /******问题详细***/
    public function questions_detail()
    {
        $questionid = I('questionid');
        $this->data = BoxModel('EduQuestions')->load($questionid);
        $this->display();
    }


    /*****试卷管理***/
    public function exams()
    {
        //$this->subject_list = BoxModel("EduSubject")->get();
        //dump(BoxModel("EduSubject"));exit();
        $where = "examstatus=1 ";
        if ($this->kw = I('kw')) {
            $where .= " and (exam like '%$this->kw%')";
        }
        if ($this->examtype = I('examtype')) {
            $where .= " and examtype='$this->examtype'";
        }
        if ($this->examid = I('examid')) {
            $where .= " and examid='$this->examid'";
        }

        if ($this->examlevel = I('examlevel')) {
            $where .= " and examlevel='$this->examlevel'";
        }
        if ($this->examauthor = I('examauthor')) {
            $where .= " and examauthor='$this->examauthor'";
        }

        if ($this->subjectid = I('subjectid')) {
            $where .= " and subjectid='$this->subjectid'";
        }

        if ($this->stime = I('stime')) {
            $where .= " and add_time>'$this->stime'";
        }
        if ($this->etime = I('etime')) {
            $where .= " and add_time<'$this->etime'";
        }

        $data = BoxModel("EduExams")->get($where, 10);
        // dump(BoxModel("EduExams"));
        //dump(BoxModel('EduQuestions'));
        $this->data = $data['list'];
        $this->page = $data['pager']->show;
        $this->display();
    }

    /*****添加试卷***/
    public function add_exams()
    {
        $this->type = I('type', "autopage");
        $this->memuNav = $this->type;
        //$this->subject_list = BoxModel("EduSubject")->tree();
								//dump($this->subject_list );
        $subject_set = array();
        foreach ($this->subject_list as $one) {
									   if($one['parent_id']==0){
              $subject_set[$one['subjectid']] = $one['subjectsetting'];
												}else{
													 $subject_set[$one['subjectid']] =$this->subject_list[$one['parent_id']]['subjectsetting'];
												}
        }
						//		dump($subject_set);
        $this->subject_set = json_encode($subject_set);
        $examid = I('examid');
        $this->data = BoxModel("EduExams")->load($examid);
        if ($this->data) {
            $this->type = $this->data['examtype'];
        }
        // dump($this->data);
        /*****保存试卷****/
        if (IS_POST) {
            $row = BoxModel("EduExams")->create();
            $row['examauthor'] = session('admin_user_name');
            foreach ($row['examsetting']['questype'] as $k => $one) {
                $questions = $row['examsetting']['questype'][$k]['questions'] = trim($one['questions'], ",");
                $row['examsetting']['questype'][$k]['questions_count'] = $questions ? count(explode(",", $questions)) : 0;
            }
            $row['examsetting'] = json_encode($row['examsetting']);

            if ($examid) {
                BoxModel("EduExams")->where(array('examid' => $examid))->save($row);
                $this->jump("保存成功", U('Questions/add_exams', array('examid' => $examid)));
            } else {
                $row['add_time'] = date("Y-m-d H:i:s");
                BoxModel("EduExams")->add($row);
                $this->jump("保存成功", U('Questions/exams'));
            }
        }
        $this->display();
    }

    /*****删除试卷***/
    public function del_exams()
    {
        $examid = I('examid');
        $this->data = BoxModel("EduExams")->load($examid);
        $examstatus = $this->data['examstatus'] == 1 ? 2 : 1;
        BoxModel("EduExams")->save(array('examid' => $examid, 'examstatus' => $examstatus));
        $this->jump("删除成功");
    }

    /******题型下的问题列表***/
    public function exams_type()
    {
        $questionid = trim(I('questionid'), ",");
        $this->data = BoxModel('EduQuestions')->ByFields("questionid", $questionid);
        $this->questiontype = I('questiontype');
        $this->display();
    }

    /****试卷预览***/
    public function preview()
    {
        $examid = I('examid');
        $this->exam = BoxModel("EduExams")->load($examid);
        //  dump($this->examid);
        $this->display();
    }


}